﻿using System;
using Database;
using System.Data;
using System.Collections.Generic;

namespace ObjectManager
{
    class LopDAO : IDatabase
    {
        private ConnectionDB cnn = null;

        public LopDTO LopDTO { get; set; }

        public LopDAO()
        {
            cnn = Connection.Connect();
            this.LopDTO = new LopDTO();
        }

        private int MaBoMon()
        {
            DataTable temp = cnn.QueryProcedure("TimMaBoMon", LopDTO.BoMon);
            int MaBM = 0;
            if (temp != null && temp.Rows.Count > 0 && !string.IsNullOrEmpty(temp.Rows[0][0].ToString()))
            {
                MaBM = (int)temp.Rows[0][0];
            }
            return MaBM;
        }

        public int Insert()
        {
            if (!string.IsNullOrEmpty(LopDTO.Ten))
            {
                return cnn.ExecuteProcedure("InsertLop", LopDTO.Ten, MaBoMon());
            }
            return -1;
        }

        public int Update()
        {
            if (!string.IsNullOrEmpty(LopDTO.Ten))
            {
                return cnn.ExecuteProcedure("UpdateLop_Ten", LopDTO.Ten, MaBoMon());
            }
            return -1;
        }

        public int Delete()
        {
            if (!string.IsNullOrEmpty(LopDTO.Ten))
            {
                return cnn.ExecuteProcedure("DeleteLop_Ten", LopDTO.Ten);
            }
            return -1;
        }

        public List<string> ListName()
        {
            List<string> result = new List<string>();
            DataTable temp = cnn.QueryProcedure("DanhSachTenLop");
            for (int i = 0; i < temp.Rows.Count; i++)
            {
                result.Add(temp.Rows[i][0].ToString());
            }
            return result;
        }

        public List<string> ListName(string BoMon)
        {
            List<string> result = new List<string>();
            DataTable temp = cnn.QueryProcedure("DanhSachTenLop_BoMon", BoMon);
            for (int i = 0; i < temp.Rows.Count; i++)
            {
                result.Add(temp.Rows[i][0].ToString());
            }
            return result;
        }

        public string FindName(int MaLop)
        {
            string result = "";
            DataTable temp = cnn.Query("select Ten from Lop where Ma = {0};", MaLop);
            if (temp != null && temp.Rows.Count > 0 && !string.IsNullOrEmpty(temp.Rows[0][0].ToString()))
            {
                result = temp.Rows[0][0].ToString();
            }
            return result;
        }

        public DataTable ListAll()
        {
            return cnn.QueryProcedure("DanhSachLop");
        }

        public DataTable ListAll(int Ma)
        {
            return cnn.QueryProcedure("DanhSachLop_Tim_Ma", Ma);
        }

        public DataTable ListAll(string Ten, string KyHieuBM, string boMon)
        {
            return cnn.QueryProcedure("DanhSachLop_Tim", Ten, KyHieuBM, boMon);
        }

        public void Search(int Ma)
        {
            DataTable temp = cnn.QueryProcedure("DanhSachLop_Tim_Ma", Ma);
            if (temp != null && temp.Rows.Count > 0)
            {
                this.LopDTO.Ma = int.Parse(temp.Rows[0][0].ToString());
                this.LopDTO.Ten = temp.Rows[0][1].ToString();
                this.LopDTO.BoMon = temp.Rows[0][3].ToString();
            }
        }

        public DataTable Ma()
        {
            return cnn.QueryProcedure("MaLop");
        }
    }
}
